
package com.bleeth.fx.component;


import com.bleeth.fx.helper.SStyleHelper;
import com.bleeth.fx.helper.StyleHelper;
import com.bleeth.fx.helper.ThemeTypeEnum;
import javafx.geometry.Pos;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Region;
import javafx.scene.shape.Circle;
import javafx.scene.text.Text;
import lombok.Builder;

import java.beans.EventHandler;

@Builder
public class MSwitch {

    @Builder.Default
    private String leftText = "";

    @Builder.Default
    private String rightText = "";

    @Builder.Default
    private String rightColor = ThemeTypeEnum.BLACK_COLOR.getValue();

    @Builder.Default
    private String leftColor = ThemeTypeEnum.BLACK_COLOR.getValue();

    @Builder.Default
    private boolean isRight = true;

    @Builder.Default
    private boolean disable = false;

    private EventHandler changeHandler;

    private Circle circle;

    private HBox box;

    private static final double DEFAULT_RADIUS = 10;

    private static final double DEFAULT_WIDTH = 50;

    private static final double DEFAULT_HEIGHT = 24;

    private static final double DEFAULT_OFFSET_VALUE = 2;

    public Region make() {

        HBox hBox = new HBox();
        hBox.setPrefHeight(50);
        hBox.setPrefWidth(200);
        hBox.setAlignment(Pos.CENTER_LEFT);
        hBox.setSpacing(10);


        Text left = new Text(leftText);
        Text right = new Text(rightText);


        box = new HBox();
        circle = new Circle(DEFAULT_RADIUS);
        box.getChildren().add(circle);

        box.setPrefHeight(DEFAULT_HEIGHT);
        box.setPrefWidth(DEFAULT_WIDTH);
        box.setMaxHeight(DEFAULT_HEIGHT);

        circle.setTranslateX(DEFAULT_WIDTH - DEFAULT_RADIUS * 2 - DEFAULT_OFFSET_VALUE);
        circle.setTranslateY(DEFAULT_OFFSET_VALUE);

        SStyleHelper.node(circle).addStyle("-fx-fill", "#ffffff").apply();
        SStyleHelper.node(box)
                .addStyle("-fx-background-color", rightColor)
                .addStyle("-fx-background-radius", DEFAULT_HEIGHT / 2+"")
                .apply();
        SStyleHelper.node(right)
                .addStyle("-fx-font-size", 14)
                .addStyle("-fx-fill", rightColor)
                .apply();
        SStyleHelper.node(left)
                .addStyle("-fx-font-size", 14)
                .addStyle("-fx-fill", leftColor)
                .apply();


        box.setOnMouseClicked(event -> {
            if (disable) {
                return;
            }
            isRight = !isRight;
            if (!isRight) {
                circle.setTranslateX(DEFAULT_OFFSET_VALUE);
                StyleHelper.bg(box).color(leftColor).radius(DEFAULT_HEIGHT / 2).make();
                StyleHelper.ft(right).fill(ThemeTypeEnum.BLACK_COLOR.getValue()).make();
                StyleHelper.ft(left).fill(leftColor).make();
            } else {
                circle.setTranslateX(DEFAULT_WIDTH - DEFAULT_RADIUS * 2 - DEFAULT_OFFSET_VALUE);
                StyleHelper.bg(box).color(rightColor).radius(DEFAULT_HEIGHT / 2).make();
                StyleHelper.ft(left).fill(ThemeTypeEnum.BLACK_COLOR.getValue()).make();
                StyleHelper.ft(right).fill(rightColor).make();
            }
        });
        if (disable) {
            box.setDisable(disable);
            if (isRight) {
                StyleHelper.bg(box).color(leftColor + "44").radius(DEFAULT_HEIGHT / 2).make();
            } else {
                StyleHelper.bg(box).color(rightColor + "44").radius(DEFAULT_HEIGHT / 2).make();
            }
        }

        hBox.getChildren().addAll(left, box, right);
        return hBox;
    }


}
